package secondStage.part3.Set.成绩排序;

import java.util.Comparator;
import java.util.TreeSet;

/**
 * @ClassName: [TreeSetTest2]
 * @Description: 就
 * @Author: [黄劲松]
 * @CreateDate: [2021-04-03]
 * @Version: [v1.0]
 */
/*案例:成绩排序
    需求:用TreeSet集合存储多个学生信息(姓名，语文成绩，数学成绩)，并遍历该集合
    要求:按照总分从高到低出现
    思路:
        定义学生类
        创建TreeSet集合对象，通过比较器排序进行排序
        创建学生对象
        把学生对象添加到集合
        遍历集合*/
public class TreeSetTest2 {
    public static void main(String[] args) {
        //通过比较器实现排序，创建对象
        TreeSet<Student> ts = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int num = o1.getSum() - o2.getSum();
                int num2 = num == 0 ? o1.getChinese() - o2.getChinese() : num;
                int num3 = num2 == 0 ? o1.getName().compareTo(o2.getName()) : num2;
                return num3;
            }
        });

        Student s1 = new Student("张三", 100, 86);
        Student s2 = new Student("李四", 99, 79);
        Student s3 = new Student("王五", 85, 96);
        Student s4 = new Student("赵柳", 92, 100);

        Student s5 = new Student("炒金麦", 95, 97);
        Student s6 = new Student("金麦", 95, 97);

        ts.add(s1);
        ts.add(s2);
        ts.add(s3);
        ts.add(s4);
        ts.add(s5);
        ts.add(s6);
        //循环遍历
        for (Student s : ts) {
            System.out.println(s.getName() + "," + s.getChinese() + "," + s.getMath() + "," + s.getSum());
        }
    }
}
